/**
 * 右上角操作按钮
 * */
import { ZoomInOutlined, ZoomOutOutlined } from '@ant-design/icons'
// =====================================================================
const { $fn, $http, $Lazy } = window
// ===================================================================== Lazy
const Export 	= $Lazy.file(()=>import('#private/common/export'))
const message 	= $Lazy.file(()=>import('@antd/message'))
// ===================================================================== 
const _ = {
	// 添加
	add: function(option){
		const opt =  {
			label: '添加',
			...option
		}
		const { param, isTable, click, code, label, disabled } = opt
		return { label, code: code === false ? '' : 'F2', disabled, loading:this.state.pagingLoading, onClick:()=> {
			const _param = isTable ? param : { formRef:this.formRef }
			this.modal?.open(_param)
			click && click()
		} }
	},
	submit: function(label, api, option){
		const opt = {
			...option
		}
		const { isKeys, msg, onOk, tip, onRefresh } = opt
		const keys = isKeys ? { disabled:!$fn.hasArray(this.state?.selectedKeys) } : null
		return { label, loading:this.state?.submitLoading, onClick:async ()=>{
			let { param } = opt
			if(tip){
				const _tip = tip?.()
				if(_tip !== true){
					return message.then(f=>f.warning(_tip))
				}
			}
			if(typeof param === 'function'){
				const rs = param(this.state?.selectedKeys, this.state?.selectedRows)
				if(rs !== false){
					param = rs
				}else{
					return message.then(f=>f.warning(msg))
				}
			}
			if($fn.isString(api) && $fn.isValid(api)){
				$http.submit(this,api,{ param }).then(data=>{
					message.then(f=>f.success(`${label}成功`))
					onOk?.(data)
					onRefresh?.(data)
				})
			}else{
				$fn.toast(' 提交 api 不存在')
			}
		}, ...option, ...keys }
	},
	// 按钮点击
	click: function(label, click, option){
		const opt = {
			pushStr:'pushRoot',
			...option
		}
		let { isKeys, pushStr, tip, scale, icon } = opt
		const keys = isKeys ? { disabled:!$fn.hasArray(this.state.selectedKeys) } : null
		if(scale === 1){
			icon = <ZoomInOutlined />
		}else if( scale === 2){
			icon = <ZoomOutOutlined />
		}
		return { label, icon, loading:this.state?.pagingLoading, onClick:()=>{
			if(tip){
				const _tip = tip?.()
				if(_tip !== true){
					return message.then(f=>f.warning(_tip))
				}
			}
			
			if(typeof click === 'string'){
				$fn[pushStr](this, click)
			}else{
				if(this.state){
					click(this.state.selectedKeys, this.state.selectedRows)
				}else{
					click()
				}
			}
		}, ...keys, ...option }
	},
	// 仅跳转
	push: function(label, to, option){
		return _.click.call(this, label, to, {...option, ghost:true})
	},
	pushTo: function(label, to, option){
		return _.click.call(this,label, to, { ghost:true, pushStr:'pushTo', ...option})
	},
	// 仅跳转
	pushCenter: function(label, to, option){
		return _.click.call(this, label, to, {...option, ghost:true, pushStr:'pushCenter' })
	},
	importTemplate: function(option){
		return _.click.call(this,'下载导入模板', '/system-settings/import-template?skip=1', { ghost:true,  ...option})
	},
	// 文件导入
	imports: function(api, option){
		let { onQuery, label='文件导入', query, refresh=true, isKeys, isMsg } = option || {}
		const keys = isKeys ? { disabled:!$fn.hasArray(this.state.selectedKeys) } : null
		return { btnText:label, type:'upload', fileType:'excel', api, imports:true, onQuery, query,isMsg, onChanged:()=>{ refresh && this?.fetch?.() }, ghost:false, loading:this.state.pagingLoading, ...option, ...keys }
	},
	// 文件导出 Excel
	exports: function(api, option){
		let { label='文件导出', code='F1', isKeys } = option || {}
		const keys = isKeys ? { disabled:!$fn.hasArray(this.state.selectedKeys) } : null
		return { label, code, loading:this.state.pagingLoading, disabled:!$fn.hasArray(this.state.data),  ghost:true, onClick:()=> {
			Export.then(f=>f.call(this, api, option))
		}, ...keys }
	},
	disviwExport: function(api, option){
		const opt = {
			isKeys:true,
			idStr:'uuid',
			...option
		}
		let { isKeys, param, idStr, query } = opt
		const { selectedKeys, selectedRows } = this.state
		const keys = isKeys ? { disabled:!$fn.hasArray(selectedKeys) } : null
		
		return { label:'导出', loading:this.state.pagingLoading, ghost:true, ...keys, onClick:()=> {
			
			param = isKeys ? {[idStr]: selectedKeys, ...param } : param
			
			query = typeof query === 'function' ? query(selectedKeys, selectedRows) : query
			
			window.$http.submit(null, api , { param: query?query:param, loading:true, loadingText:'文件导出中...' }).then(({url})=>{
				window.location.href = url
				message.then(f=>f.success('文件导出成功'))
			})
		} }
	},
	// 返回
	back: function(option){
		const { refresh, refreshName, param } = option || {}
		return { label:'返回', loading:this?.state?.pagingLoading, ghost:true, onClick:()=> {
			if(refresh){
				$fn.fresh.back.call(this,refreshName, param)
			}else{
				$fn.back(this)
			}
		} }
	},
	setTableHeader: function(){
		return { label:'设置表头', loading:this.state.pagingLoading, ghost:true, onClick:()=> { this.table.openHeader(this.cols)  } }
	},
	// 打印
	printer:function(option){
		let { batch, code = 50106, data, label='批量打印' }  = option || {}
		const keys = batch ? { disabled:!$fn.hasArray(this.state.selectedKeys) } : null
		data = batch ? this.state.selectedRows : data
		return { label, code, batch, loading:this?.state?.pagingLoading, ...keys, data, type:'printer', ...option }
	}
}
export default _